package com.atlc.preview.project.net.server;

import com.atlc.preview.project.model.Pages;
import javafx.stage.FileChooser;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;
import java.util.stream.Collectors;

public class Test extends JFrame {

    private Font font = new Font("微软雅黑", Font.BOLD, 25);
    private String paperPath = null;

    private JLabel JLTime = new JLabel("  倒计时:");
    private JLabel JLHour = new JLabel();
    private JLabel JLMinute = new JLabel();
    private JLabel JLSeconds = new JLabel();

    private JLabel testName = new JLabel("");
    private JLabel reference = new JLabel("参考人数：");
    private JLabel referenceSum = new JLabel("应到人数：");
    private JLabel questionNumber = new JLabel("题目总数：");
    private JLabel submitNumber = new JLabel("已提交：");

    // 表头（列名）
    private String[] columnNames = {"序号", "座位号", "姓名", "已答题数", "操作状态"};

    // 表格所有行数据
    private Object[][] rowData = {
            {1, 1, "张三", 3, "答题中"},
            {2, 2, "John", 13, "答题中"},
            {3, 3, "Sue", 20, "已交卷"},
            {4, 4, "Jane", 3, "答题中"},
            {5, 5, "Joe_05", 3, "答题中"},
            {6, 6, "Joe_06", 3, "答题中"},
            {7, 7, "Joe_07", 3, "答题中"},
            {8, 8, "Joe_08", 3, "答题中"},
            {8, 8, "Joe_09", 3, "答题中"},
            {10, 10, "Joe_10", 3, "答题中"},
            {11, 11, "Joe_11", 3, "答题中"},
            {12, 12, "Joe_12", 3, "答题中"},
            {13, 13, "Joe_13", 3, "答题中"},
            {14, 14, "Joe_14", 3, "答题中"},
            {15, 15, "Joe_15", 3, "答题中"},
            {16, 16, "Joe_16", 3, "答题中"},
            {17, 17, "Joe_17", 3, "答题中"},
            {18, 18, "Joe_18", 3, "答题中"},
            {19, 19, "Joe_19", 3, "答题中"},
            {20, 20, "Joe_20", 3, "答题中"}
    };

    // 创建一个表格，指定 表头 和 所有行数据
    JTable table = new JTable(rowData, columnNames);

    JMenuBar menuBar = new JMenuBar();//创建菜单栏对象
    JMenu testPaper = new JMenu("试卷管理");
    JMenu examine = new JMenu("考生管理");
    JMenu grade = new JMenu("成绩管理");
    JMenuItem addPaper = new JMenuItem("添加试卷");
    JMenuItem loadExamine = new JMenuItem("载入考生");
    JMenuItem judgeGrade = new JMenuItem("判分");
    JMenuItem saveGrade = new JMenuItem("保存成绩");

    BorderLayout bl = new BorderLayout(50, 50);
    JPanel paperChoose = new JPanel();
    JPanel testImformation = new JPanel(bl);
    JPanel studentInformation = new JPanel();
    JPanel state = new JPanel();
    JPanel JPTime = new JPanel();

    JTable paperList = new JTable();

    JButton sure = new JButton("确定");
    JButton cancel = new JButton("取消");

    FileChooser fileChooser = new FileChooser();

    public long time = 3600;

    public static void main(String[] args) {
        new Test();
    }

    Test(){
        this.setTitle("监控端");
        this.setSize(1500, 800);
        this.setLocationRelativeTo(null);  //将此窗口置于屏幕的中央
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLayout(new BorderLayout(20, 20));

//        设置菜单
        setJMenuBar(menuBar);
        menuBar.add(testPaper);
        menuBar.add(examine);
        menuBar.add(grade);
        testPaper.add(addPaper);
        examine.add(loadExamine);
        grade.add(judgeGrade);
        grade.add(saveGrade);

//        设置字体
        sure.setFont(font);
        cancel.setFont(font);
        JLTime.setFont(font);
        JLHour.setFont(font);
        JLMinute.setFont(font);
        JLSeconds.setFont(font);
        testName.setFont(font);
        reference.setFont(font);
        referenceSum.setFont(font);
        questionNumber.setFont(font);
        submitNumber.setFont(font);
        table.setFont(font);
        testPaper.setFont(font);
        examine.setFont(font);
        grade.setFont(font);
        addPaper.setFont(font);
        loadExamine.setFont(font);
        judgeGrade.setFont(font);
        saveGrade.setFont(font);
        fileChooser.setTitle("添加试卷");

//        paperChoose.add(sure);
//        paperChoose.add(cancel);
        paperChoose.add(paperList);
        this.add(paperChoose, BorderLayout.WEST);

        testImformation.add(testName, BorderLayout.NORTH);

        time();
        JPTime.add(JLTime);
        JPTime.add(JLHour);
        JPTime.add(JLMinute);
        JPTime.add(JLSeconds);
        testImformation.add(JPTime, BorderLayout.EAST);

        state.add(reference);
        state.add(referenceSum);
        state.add(questionNumber);
        state.add(submitNumber);
        testImformation.add(state, BorderLayout.CENTER);

        this.add(testImformation, BorderLayout.NORTH);

        // 设置表头
        table.getTableHeader().setFont(font);// 设置表头名称字体样式
        table.getTableHeader().setForeground(Color.RED);// 设置表头名称字体颜色
        table.getTableHeader().setResizingAllowed(true);// 设置允许手动改变列宽
        table.getTableHeader().setReorderingAllowed(false);// 设置不允许拖动重新排序各列
        table.setRowHeight(50);//设置行高
        table.setPreferredScrollableViewportSize(new Dimension(1400, 400));// 设置滚动面板视口大小（超过该大小的行数据，需要拖动滚动条才能看到）
        JScrollPane scrollPane = new JScrollPane(table); // 把 表格 放到 滚动面板 中（表头将自动添加到滚动面板顶部）
        studentInformation.add(scrollPane);// 添加 滚动面板 到 内容面板
        this.add(studentInformation, BorderLayout.CENTER);

        setVisible(true);

        judgePaper();
    }

//    设置监听器
    public void judgePaper(){
//        添加试卷
        addPaper.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

            }
        });
//        载入考生
        loadExamine.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

            }
        });
//        判分
        judgeGrade.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

            }
        });
//        保存成绩
        saveGrade.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

            }
        });
    }

    //     定时器
    private void time() {
        java.util.Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            public void run() {
                time--;
                long hour = time / 3600;
                long minute = (time - hour * 3600) / 60;
                long seconds = time - hour * 3600 - minute * 60;
                JLHour.setText(hour + "时");
                JLMinute.setText(minute + "分");
                JLSeconds.setText(seconds + "秒");
                if (time == 0) {
                    judgeGrade.doClick(); // 自动点击“提交试卷”按钮
                }
            }
        }, 0, 1000);  // 延时0秒触发，以后每间隔1秒循环触发
    }

}
